cmake_minimum_required(VERSION 3.16)

project(instrdrawer
    VERSION 1.0
    DESCRIPTION "XL3000 series instrdrawer program"
    LANGUAGES C
    )


enable_language(ASM)
option(BUILD_TESTS "Build library tests" OFF)

set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON)
if(APPLE)
    list(APPEND CMAKE_INSTALL_RPATH "@executable_path")
elseif(UNIX)
    list(APPEND CMAKE_INSTALL_RPATH "\$ORIGIN")
endif()

if(UNIX)
    if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
        set(CMAKE_INSTALL_PREFIX "/usr/local/instrdrawer" CACHE PATH "Install prefix" FORCE)
    endif()
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(Git)
if (Git_FOUND)
    message("git version ${GIT_VERSION_STRING}")
    execute_process(
        COMMAND "${GIT_EXECUTABLE}" describe --match="" --always --dirty --abbrev=40
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE PROJECT_REPOSITORY
    )
    string(STRIP ${PROJECT_REPOSITORY} PROJECT_REPOSITORY)
    message(${PROJECT_REPOSITORY})
endif()

enable_testing()


add_compile_definitions(GD32F405)

add_subdirectory(application)
add_subdirectory(bootloader)
add_subdirectory(Drivers)

if(BUILD_TESTS)
    add_subdirectory(tests)
endif()

#install(TARGETS drivers instrdrawer DESTINATION .)
#install(FILES ${HEX_FILE}
#    DESTINATION ../upgrade/upgrade
#    COMPONENT config EXCLUDE_FROM_ALL
#    )
